﻿using FasterWPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using winForm = System.Windows.Forms;
using sysDataChart = System.Windows.Forms.DataVisualization.Charting;
using System.Windows.Media;
using System.Windows;
using System.Windows.Input;
using System.Reactive.Linq;
using FasterWPFDemo.UserControls;

namespace FasterWPFDemo
{
    public class TabItem05 : UserControl, IUserControlGuid
    {
        private string _userCtlGuid = null;
        winForm.Integration.WindowsFormsHost host1 = null;

        public string UserControlGuid
        {
            get
            {
                return _userCtlGuid;
            }

            set
            {
                _userCtlGuid = value;
            }
        }

        public TabItem05()
        {
            //InitializeComponent(); XAML removed. Initialize via code-behind.

            this.Initialize(ContainerType.VerticalPanel);

            this.Width = 450D;
            this.Height = 495D;

            this.BeginSettings<UserControl>().SetItemBorderColorAndThickness<UserControl>(Brushes.Gray, new Thickness(3))
                .SetSelectorClass<UserControl>(0, 0, "tabLabel")
                .SetFontOnLabel<UserControl>(0, 0, "Segoe UI", 12D, FontWeights.Normal, FontStyles.Normal)
                .SetTextColorForLabel<UserControl>(0, 0, Brushes.White)
                .Set<Label, UserControl>(0, 3, "Height", 10D)
                .EndSettings();
        }

        public void DisposeWinFormHost()
        {
            if (host1 != null)
            {
                host1.Dispose();
                host1 = null;
            }            
        }

        public void LoadUserControl()
        { 
            string myBorderGuid0 = Guid.NewGuid().ToString();

            host1 = new winForm.Integration.WindowsFormsHost();
            host1.Height = 300;
            host1.Width = 250;
            host1.VerticalAlignment = VerticalAlignment.Top;

            // Initialize the Chart object
            sysDataChart.Chart Chart1 = new sysDataChart.Chart();
            Chart1.Height = 300;
            Chart1.Width = 250;

            // Add a chart area.
            Chart1.ChartAreas.Add("Default");

            // Add a series with some data points.
            sysDataChart.Series series = new sysDataChart.Series();

            series.Color = System.Drawing.Color.Green;
            Chart1.ChartAreas[0].AxisX.Title = "Years";
            Chart1.ChartAreas[0].AxisX.CustomLabels.Add(1, sysDataChart.DateTimeIntervalType.Years);
            Chart1.ChartAreas[0].AxisY.Title = "# Vacations";

            series.Points.Add(new sysDataChart.DataPoint(1990, 5));
            series.Points.Add(new sysDataChart.DataPoint(1991, 15));
            series.Points.Add(new sysDataChart.DataPoint(1992, 7));
            Chart1.Series.Add(series);

            // Add the chart to the Windows Form Host.
            host1.Child = Chart1;

            Border flatButtonChrt = new Border();
            flatButtonChrt.VerticalAlignment = VerticalAlignment.Top;
            flatButtonChrt.Initialize(double.NaN, 35D, ContainerType.HorizontalPanel, Brushes.Indigo, new Thickness(2), Brushes.PowderBlue, new CornerRadius(10), new Thickness(0), new Thickness(3));
            flatButtonChrt.BeginSettings<Border>()
                .SetTextPadding<Border>(0, 0, new Thickness(4))
                .EndSettings<Border>();

            List<sysDataChart.DataPoint> datList = new List<sysDataChart.DataPoint>();

            datList.Add(new sysDataChart.DataPoint(1993, 8));
            datList.Add(new sysDataChart.DataPoint(1994, 9));
            datList.Add(new sysDataChart.DataPoint(1995, 10));
            datList.Add(new sysDataChart.DataPoint(1996, 11));
            datList.Add(new sysDataChart.DataPoint(1997, 5));
            datList.Add(new sysDataChart.DataPoint(1998, 15));
            datList.Add(new sysDataChart.DataPoint(1999, 20));
            datList.Add(new sysDataChart.DataPoint(2000, 13));
            datList.Add(new sysDataChart.DataPoint(2001, 14));
            datList.Add(new sysDataChart.DataPoint(2002, 5));
            datList.Add(new sysDataChart.DataPoint(2003, 17));
            datList.Add(new sysDataChart.DataPoint(2004, 17));
            datList.Add(new sysDataChart.DataPoint(1990, 5));
            datList.Add(new sysDataChart.DataPoint(1991, 15));
            datList.Add(new sysDataChart.DataPoint(1992, 7));

            bool tog = false;
            IDisposable disp = null;
            int chart1Counter = 0;
            flatButtonChrt.SetMouseOverColorOnBorder(Brushes.Orange);

            flatButtonChrt.SubscribeMouseSingleClickOnBorder<Border>((o, ev) =>
            {
                if (tog == false)
                {
                    disp = Observable.Timer(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(3)).Subscribe(t =>
                    {
                        Dispatcher.RunInUiThread(() =>
                        {
                            if (chart1Counter == 15)
                            {
                                chart1Counter = 0;
                            }
                            series.Points.Clear();
                            series.Points.Add(datList[chart1Counter++]);
                            series.Points.Add(datList[chart1Counter++]);
                            series.Points.Add(datList[chart1Counter++]);
                            Chart1.Invalidate();
                            host1.InvalidateVisual();

                        });
                    });

                    BorderExt.ChangeBackgroundColor(flatButtonChrt, Brushes.Khaki);
                    flatButtonChrt.GetTextBlock<Border>(myBorderGuid0, 0, 0).Text = "Stop Animating Chart";
                    tog = true;
                }
                else
                {
                    disp.Dispose();
                    BorderExt.ChangeBackgroundColor(flatButtonChrt, Brushes.PowderBlue);
                    flatButtonChrt.GetTextBlock<Border>(myBorderGuid0, 0, 0).Text = "Start Animating Chart";
                    tog = false;
                }
            });
            flatButtonChrt.BeginComposite(myBorderGuid0).AddText<Border>(0, 0, "Start Animating Chart").EndComposite<Border, BorderArgs>(null);

            this.BeginComposite<UserControl>(_userCtlGuid)
                      .AddLabel<UserControl>(0, 0, "Tab Item 5 -  Chart Example", Brushes.Green)
                      .AddExisting<winForm.Integration.WindowsFormsHost, UserControl>(0, 1, host1)
                      .AddExisting<Border, UserControl>(0, 2, flatButtonChrt)
                      .AddLabel<UserControl>(0, 3, "", Brushes.Green)
                      .AddFlatButton<UserControl>(0, 4, 120D, 98D,
                "Remove this\n UserControl!", Brushes.GhostWhite, new Thickness(2), Brushes.Green, new CornerRadius(0), new Thickness(2), new Thickness(3), Brushes.LightGreen,
                (o, ev) =>
                {
                    disp.Dispose(); //need to dispose?
                    host1.Dispose();

                    //Retrieve Action from Global Store on Application object
                    GlobalStoreService.GetFromGlobalStore<Action<object, RoutedEventArgs>>(GlobalStoreService.TabControlGuids.Five, "TabItem5RemoveUserControl")(o, (RoutedEventArgs)ev);


                }).EndComposite<UserControl, UserControlArgs>(null);
        }

    }
}
